//
//  RFATreeEnumerator.m
//  RFA Manager
//
//  Created by Torsten Kammer on 26.03.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "RFATreeEnumerator.h"


@implementation RFATreeEnumerator

- (id)initWithNode:(RFATreeNode *)node
{
	if (![super init]) return nil;
	
	childEnumerator = [[node valueForKeyPath:@"children"] objectEnumerator];
	subEnumerator = nil;
	
	return self;
}

- (id)nextObject
{
	if (subEnumerator)
	{
		RFATreeNode *result = [subEnumerator nextObject];
		if (!result) subEnumerator = nil;
		else return result;
	}
	
	RFATreeNode *nextObject = [childEnumerator nextObject];
	if (!nextObject) return nil;
	if (!nextObject.isLeaf)
	{
		subEnumerator = [[RFATreeEnumerator alloc] initWithNode:nextObject];
		return [subEnumerator nextObject];
	}
	return nextObject;
}

@end
